<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.user.inc.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/FnOrder.class.php');
require_once(__PHY_LIB.'/func/Cart.func.php');
require_once(__PHY_LIB.'/func/Shipping.func.php');
require_once(__PHY_LIB.'/func/Payment.func.php');

/// 初始化 ///
$rReq = validate($_REQUEST, 'action:e[none,create]');
$rMsg = array();

if ('create' == $rReq['action']) {
	$bSucc = false;
	do {
		$rP = validate($_POST, 'sign:u|address:u|mobile:u|phone:u|email:u|postcode:u|payment_id:n|ship_id:n');
		$oTpl->assign('rP', $rP);
		if (!$rP['sign']) { break; }
		if (!$rP['address']) { break; }
		if (!$rP['mobile']) { break; }
		if (!$rP['payment_id']) { break; }
		if (!$rP['ship_id']) { break; }
		/// 库存检查 ///
		$rCart = cart_fetch();
		$bError = false;
		foreach ($rCart as $k => $r) {
			$oWork = new uWork($r['ItemID']);
			$rStock = $oWork->getSku($r['SizeID'], $r['KuangID']);
			if ($rStock['stock'] < $r['Quantity']) { // 库存不够
				$rMsg['action'] .= 'Out of Stock.';
				$bError = true;
				break;
			}
		}
		if ($bError) { break; }
		/// 生成订单 ///
		$sNow = date('Y-m-d H:i:s');
		$oOrder = new Order();
		$oOrder->set('owner_type', $_The['UserType']);
		$oOrder->set('owner_id', $_The['User']['id']);
		$oOrder->set('time_added', $sNow);
		$oOrder->set('payment_id', $rP['payment_id']);
		$oOrder->set('payment_money', $_SESSION['MONEY']);
		$oOrder->set('payment_method', payment_method($rP['payment_id']));
		$oOrder->set('ship_id', $rP['ship_id']);
		$oOrder->set('ship_method', ship_method($rP['ship_id']));
		$oOrder->set('ship_price', ship_price($rP['ship_id']));
		$oOrder->set('ship_sign', $rP['sign']);
		$oOrder->set('ship_address', $rP['address']);
		$oOrder->set('ship_mobile', $rP['mobile']);
		$oOrder->set('ship_phone', $rP['phone']);
		$oOrder->set('ship_email', $rP['email']);
		$oOrder->set('ship_postcode', $rP['postcode']);
		$oOrder->set('status', 'unpaid');
		$oOrder->set('total_price', ship_price($rP['ship_id']));
		if (!$oOrder->insert()) { $sMsg['action'] = 'failed to create order.'; break; }
		/// 给订单加入商品 ///
		foreach ($rCart as $k => $r) {
			$oOrder->addWork($r['ItemID'], $r['SizeID'], $r['KuangID'], $r['Quantity']); // 其中已包含库存操作
		}		
		$oOrder->update();
		/// 清空购物车 ///
		cart_delete_all();
		header("Location: confirm.php?order=".$oOrder->getID()); exit();
	} while (false);
}

/// 支付方式，快递公司 ///
$oTpl->assign('rShipMethods', $_ShipMethods);
$oTpl->assign('rPayments', $_Payments);

/// 得到购物车信息 ///
$rCart = cart_fetch();
/// 购物车里的作品列表 ///
foreach ($rCart as $k => $r) {
	$oWork = new uWork($r['ItemID']);
	$oWork->set('size_id', $r['SizeID']);
	$oWork->set('kuang_id', $r['KuangID']);
	$oWork->set('Sku', "{$r['ItemID']}_{$r['SizeID']}_{$r['KuangID']}");
	if (0 < $r['SizeID']) {
		$oWorkSize = new uWorkSize($r['SizeID']);
		$oWork->set('size_title', $oWorkSize->get('title'));
		$oWork->set('size_title_en', $oWorkSize->get('title_en'));
	}
	$oWork->getSku($r['SizeID'], $r['KuangID'], true);
	$rCart[$k]['Item'] = $oWork->getData();
}
$oTpl->assign('rCart', $rCart);
//@ echo '<pre>'; print_r($rCart); echo '<pre>';

$oTpl->assign('rMsg', $rMsg);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
